package com.skyolder.crutch.word.raw;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.word.token.raw.IdentifierTokenCreator;
import com.skyolder.crutch.word.token.raw.KeyWordTokenTokenCreator;
import com.skyolder.crutch.word.token.raw.NextLineTokenCreator;
import com.skyolder.crutch.word.token.raw.NotesTokenCreator;
import com.skyolder.crutch.word.token.raw.RawToken;
import com.skyolder.crutch.word.token.raw.SpaceTokenCreator;
import com.skyolder.crutch.word.token.raw.StringTokenCreator;
import com.skyolder.crutch.word.token.raw.SymbolTokenCreator;

public class TokenPiper {
	private BufferCharQueue bufferCharQueue = null;
	private NotesTokenCreator notesTokenCreator = new NotesTokenCreator();;
	private StringTokenCreator stringTokenCreator = new StringTokenCreator();
	private NextLineTokenCreator nextLineTokenCreator = new NextLineTokenCreator();
	private SymbolTokenCreator symbolTokenCreator=new SymbolTokenCreator();
	private KeyWordTokenTokenCreator keyWordTokenTokenCreator=new KeyWordTokenTokenCreator();
	private IdentifierTokenCreator identifierTokenCreator=new IdentifierTokenCreator();
	private SpaceTokenCreator spaceTokenCreator=new SpaceTokenCreator();
	public TokenPiper(BufferCharQueue bufferCharQueue) {
		this.bufferCharQueue = bufferCharQueue;
	}

	public RawToken nextRawToken() throws GrammarError {

		RawToken rawToken = nextLineTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}
		rawToken=spaceTokenCreator.tryCreateToken(bufferCharQueue);
		if(rawToken!=null){
			return rawToken;
		}
		 rawToken = notesTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}
		rawToken = stringTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}
		rawToken = symbolTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}
		rawToken = keyWordTokenTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}
		rawToken = identifierTokenCreator.tryCreateToken(bufferCharQueue);
		if (rawToken != null) {
			return rawToken;
		}		
		return null;
	}
}
